//
//  OrderVC.m
//  YangWifi
//
//  Created by zuoxueliangduyuanzhuo on 2017/6/10.
//  Copyright © 2017年 zuoxueliangduyuanzhuo. All rights reserved.
//

#import "OrderVC.h"
#import "LoginVC.h"
#import "ProductListVC.h"
@interface OrderVC ()<UITableViewDelegate,UITableViewDataSource>


@end

@implementation OrderVC

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (![AccountHanler isLogin]) {
        self.noDataImgName = @"no_login";
        self.noDataTitle = @"亲！你还没有登录哦";
        self.btnTitle = @"登    录";
        self.btnBgImgName = @"none_data_btn_bg_normal";
        self.btnBgSelectedImgName = @"none_data_btn_bg_selected";
        self.noDataDetailTitle = nil;
        [self.xlTableV reloadData];
    }
    
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"" object:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"订单";
    self.xlTableV.dataSource = self;
    self.xlTableV.delegate = self;
    
    if ([AccountHanler isLogin]) {
        
        self.noDataImgName = @"";
        self.noDataTitle = @"";
        self.noDataDetailTitle = nil;
        
        self.btnTitle = nil;
        self.btnBgImgName = nil;
        self.btnBgSelectedImgName = nil;
        [self getOrderList];

    }else{
        self.noDataImgName = @"no_login";
        self.noDataTitle = @"亲！你还没有登录哦";
        self.btnTitle = @"登    录";
        self.btnBgImgName = @"none_data_btn_bg_normal";
        self.btnBgSelectedImgName = @"none_data_btn_bg_selected";
        self.noDataDetailTitle = nil;
    }
    
    [self buildNavRightItemWithTitle:@"套餐" action:@selector(productListClick:)];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOrderList) name:@"loginHandlerOrder" object:nil];
}

- (void)productListClick:(UIButton *)btn
{
    ProductListVC *vc = [[ProductListVC alloc] initWithNibName:@"XLBaseEmptyTableVC" bundle:nil];
    [self showViewController:vc sender:nil];
}

- (void)getOrderList
{
    NSDictionary *params = @{
                             @"type": @"OSWIFI_ORDERLIST_MOBILE",
                             @"token":[AccountHanler token],
                             @"requestEntity": @{
                                     @"telephone":[AccountHanler telPhone],
                                     @"logicstate":@"0",
                                 }
                             };
    @WeakObj(self);
    [MHNetworkManager postReqeustWithURL:nil params:params successBlock:^(NSDictionary *returnData) {
        XLBaseModel *resultData = [XLBaseModel mj_objectWithKeyValues:returnData];
        if (resultData.resultFlag) {
            
            if (self.data.count == 0) {
                
                [self.data removeAllObjects];
                self.noDataImgName = @"no_data";
                self.noDataTitle = @"暂无订单";
                self.noDataDetailTitle = @"";
                
                self.btnTitle = @"快来下单吧";
                self.btnBgImgName = @"none_data_btn_bg_normal";
                self.btnBgSelectedImgName = @"none_data_btn_bg_selected";
                
            }else{
                
                self.noDataImgName = @"";
                self.noDataTitle = @"";
                self.noDataDetailTitle = nil;
                
                self.btnTitle = nil;
                self.btnBgImgName = nil;
                self.btnBgSelectedImgName = nil;
                
            }
            
        }else{

            [Weakself.view makeToast:@"获取订单列表失败"];
            
            self.noDataImgName = @"no_network";
            self.noDataTitle = @"哎呀！系统发生错误";
            self.btnTitle = @"刷新一下试试";
            self.btnBgImgName = @"none_data_btn_bg_normal";
            self.btnBgSelectedImgName = @"none_data_btn_bg_selected";
            self.noDataDetailTitle = nil;

        }
        
        [Weakself.xlTableV reloadData];

    } failureBlock:^(NSError *error) {
        
        [Weakself.view makeToast:@"获取订单列表失败"];
        [self.data removeAllObjects];
        self.noDataImgName = @"no_network";
        self.noDataTitle = @"哎呀！系统发生错误";
        self.btnTitle = @"刷新一下试试";
        self.btnBgImgName = @"none_data_btn_bg_normal";
        self.btnBgSelectedImgName = @"none_data_btn_bg_selected";
        self.noDataDetailTitle = nil;
        [Weakself.xlTableV reloadData];

   
    } showHUD:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"XLCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.data.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}


- (void)buttonEvent:(NSString *)title
{
    if ([title isEqualToString:@"登    录"]) {
        LoginVC *loginVC = [[LoginVC alloc] initWithNibName:@"LoginVC" bundle:nil];
        loginVC.isNeedOrderNoti = NO;
        loginVC.isNeedHomeNoti = YES;
        
        @WeakObj(self);
        loginVC.loginHandler = ^{
            
            [Weakself getOrderList];
        };
        XLNavigationVC *nav = [[XLNavigationVC alloc] initWithRootViewController:loginVC];
        [self presentViewController:nav animated:YES completion:^{
            
        }];
    }else if ([title isEqualToString:@"刷新一下试试"]){
        [self getOrderList];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
